package java8.chapter03.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import static java.util.Comparator.comparing;

import java8.chapter03.test.Lambdas.Apple;

public class Sorting {

	static class AppleComparator implements Comparator<Apple> {

		@Override
		public int compare(Apple a1, Apple a2) {
			return a1.getWeight().compareTo(a2.getWeight());
		}

	}

	public static void main(String[] args) {
		List<Apple> inventory = new ArrayList<>();
		inventory.addAll(Arrays.asList(new Apple(80, "green"), new Apple(155, "green"), new Apple(120, "red")));
		
		inventory.sort(new AppleComparator());
		System.out.println(inventory);
		
		inventory.set(1, new Apple(30, "green"));
		inventory.sort(new Comparator<Apple>() {

			@Override
			public int compare(Apple a1, Apple a2) {
				return a1.getWeight().compareTo(a2.getWeight());
			}
			
		});
		System.out.println(inventory);
		
		inventory.set(1, new Apple(20, "red"));
		inventory.sort((a1, a2) -> a1.getWeight().compareTo(a2.getWeight()));
		System.out.println(inventory);
		
		inventory.set(1, new Apple(10, "red"));
		inventory.sort(comparing(Apple::getWeight));
		System.out.println(inventory);
	}

}